﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Spring.Web.Mvc;

namespace Workflow.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

	public class MvcApplication : SpringMvcApplication//System.Web.HttpApplication  //
    {
		protected override void RegisterRoutes(RouteCollection routes)
        {
            //default
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.IgnoreRoute("ViewTiff.aspx");
            routes.IgnoreRoute("ViewImg.aspx");
            routes.IgnoreRoute("ViewTiffActiveX.aspx");
            
            //routes.MapRoute(
            //    "LogOn", // Route name 4               
            //    "Account/{action}", // URL with parameters 5                
            //    new { controller = "Account", action = "LogOn" } // Parameter defaults 6                 
            //    );

            //NonUseage
            routes.MapRoute(
            "GP", // 路由名称  
            "PTP/{action}/{id}", // 带有参数的 URL  
              new { controller = "PTP", action = "Inquiry", id = UrlParameter.Optional },//参数默认值  
              new { controller = @"PTP" }
             );

            routes.MapRoute(

             "Localization", // 路由名称  

             "{lang}/{controller}/{action}/{id}", // 带有参数的 URL  
              
               new { controller = "Home", action = "Index", id = UrlParameter.Optional },//参数默认值  
             new { lang = @"\w{2,3}(-\w{4})?(-\w{2,3})?$" }
              );

          
         
			routes.MapRoute(
				"Default", // Route name
				"{controller}/{action}/{id}", // URL with parameters
				new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
			);
		    routes.MapRoute("catch-all", "{*catchall}", new {controller = "Error", action = "NotFound"}
                );

        }

        protected void Application_Start()
        {
         
            
            AreaRegistration.RegisterAllAreas();

            RouteTable.Routes.RouteExistingFiles = true;

            RegisterRoutes(RouteTable.Routes);

            //RegisterGlobalizationRoutes();
        }
        //private void RegisterGlobalizationRoutes()
        //{

        //    if (RouteTable.Routes == null)
        //        return;

        //    RouteCollection rc = new RouteCollection();

        //    //skip IgnoreRouteInternal
        //    var routes = RouteTable.Routes.SkipWhile(p => (p.GetType().Name == "IgnoreRouteInternal"));

        //    int insertpoint = RouteTable.Routes.Count() - routes.Count();

        //    foreach (var r in routes)
        //    {
        //        Route item = (r as Route);
        //        Route newitem = new Route(
        //            //string.Format(@"{ci}/{0}",item.Url),
        //            @"{ci}/" + item.Url,
        //            new MvcRouteHandler());
        //        newitem.Defaults = new RouteValueDictionary(item.Defaults);

        //        newitem.Constraints = new RouteValueDictionary(item.Constraints);
        //        newitem.Constraints.Add("ci", new CulturePrefixRule());

        //        newitem.DataTokens = new RouteValueDictionary();
        //        newitem.DataTokens["Namespaces"] = item.DataTokens["Namespaces"];
        //        rc.Add(newitem);
        //    }
        //    foreach (var c in rc)
        //    {
        //        RouteTable.Routes.Insert(insertpoint++, c);
        //    }
        //}

        //private class CulturePrefixRule : IRouteConstraint
        //{
        //    IEnumerable<string> cultureConllection = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(p => p.Name.ToLower());
        //    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        //    {
        //        if (values[parameterName] != null)
        //            return cultureConllection.Contains(values[parameterName].ToString().ToLower());
        //        else
        //            return false;
        //    }
        //}
    }
}